namespace Atom.Utility.Data
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using Atom.Utility;

    public static class DataAccessExtensive
    {
        public static DataTable ExecuteDataTable(Database dataAccess, DbCommand cmd)
        {
            return ExecuteDataTable(dataAccess, cmd, null);
        }

        public static DataTable ExecuteDataTable(Database dataAccess, DbCommand cmd, DbTransaction trans)
        {
            DataTable table = new DataTable();
            DataSet set = (trans != null) ? dataAccess.ExecuteDataSet(cmd, trans) : dataAccess.ExecuteDataSet(cmd);
            if ((set != null) && (set.Tables.Count > 0))
            {
                table = set.Tables[0];
            }
            return table;
        }

        public static DataSet ExecuteDataSet(Database dataAccess, DbCommand cmd, DbTransaction trans)
        {
            DataSet set = (trans != null) ? dataAccess.ExecuteDataSet(cmd, trans) : dataAccess.ExecuteDataSet(cmd);

            return set;
        }

        public static List<T> ExecuteListEntity<T>(Database dataAccess, DbCommand cmd, Action<T, IDataReader> action) where T : class, new()
        {
            return ExecuteListEntity<T>(dataAccess, cmd, action, null);
        }

        public static List<T> ExecuteListEntity<T>(Database dataAccess, DbCommand cmd, Action<T, IDataReader> action, DbTransaction trans) where T : class, new()
        {
            List<T> list = new List<T>();
            if (action != null)
            {
                using (IDataReader reader = (trans == null) ? dataAccess.ExecuteReader(cmd) : dataAccess.ExecuteReader(cmd, trans))
                {
                    while (reader.Read())
                    {
                        T local = Activator.CreateInstance<T>();
                        action(local, reader);
                        list.Add(local);
                    }
                }
            }
            return list;
        }

        public static List<T> ExecuteListEntity<T>(Database dataAccess, DbCommand cmd) where T : class, new()
        {
            List<T> list = new List<T>();

            using (IDataReader reader = dataAccess.ExecuteReader(cmd) )
            {
                while (reader.Read())
                {
                    //T local = Activator.CreateInstance<T>();
                    //action(local, reader);

                    DynamicBuilder<T> builder = DynamicBuilder<T>.CreateBuilder(reader);
                    T local = builder.Build(reader);
                    
                    list.Add(local);
                }
            }

            return list;
        }

        public static DataRow ExecuteSingleData(Database dataAccess, DbCommand cmd)
        {
            return ExecuteSingleData(dataAccess, cmd, null);
        }

        public static DataRow ExecuteSingleData(Database dataAccess, DbCommand cmd, DbTransaction trans)
        {
            DataRow row = null;
            DataTable table = ExecuteDataTable(dataAccess, cmd, trans);
            if ((table != null) && (table.Rows.Count > 0))
            {
                row = table.Rows[0];
            }
            return row;
        }

        public static T ExecuteSingleEntity<T>(Database dataAccess, DbCommand cmd, Action<T, IDataReader> action) where T : class, new()
        {
            return ExecuteSingleEntity<T>(dataAccess, cmd, action, null);
        }

        public static T ExecuteSingleEntity<T>(Database dataAccess, DbCommand cmd) where T : class, new()
        {
            T local = default(T);
            using (IDataReader reader = dataAccess.ExecuteReader(cmd))
            {
                if (reader.Read())
                {
                    //   local = Activator.CreateInstance<T>();
                    DynamicBuilder<T> builder = DynamicBuilder<T>.CreateBuilder(reader);
                    local = builder.Build(reader);
                }
            }

            return local;
        }

        public static T ExecuteSingleEntity<T>(Database dataAccess, DbCommand cmd, Action<T, IDataReader> action, DbTransaction trans) where T : class, new()
        {
            T local = default(T);
            if (action != null)
            {
                using (IDataReader reader = (trans == null) ? dataAccess.ExecuteReader(cmd) : dataAccess.ExecuteReader(cmd, trans))
                {
                    if (reader.Read())
                    {
                        local = Activator.CreateInstance<T>();
                        action(local, reader);
                    }
                }
            }
            return local;
        }

        public static bool ExecuteTransaction(Database dataAccess, TransactionAction action, Action<Exception> logAction)
        {
            bool flag = false;
            if (action != null)
            {
                using (DbConnection connection = dataAccess.CreateConnection())
                {
                    connection.Open();
                    DbTransaction trans = connection.BeginTransaction();
                    try
                    {
                        if (action(trans))
                        {
                            trans.Commit();
                        }
                        else
                        {
                            trans.Rollback();
                        }
                        flag = true;
                    }
                    catch (Exception exception)
                    {
                        if (logAction != null)
                        {
                            logAction(exception);
                        }
                        trans.Rollback();
                        throw exception;
                    }
                    connection.Close();
                }
            }
            return flag;
        }
    }
}
